package com.telkomsigma.spring.hibernate.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.telkomsigma.spring.hibernate.persistence.domain.FootballTeam;
import com.telkomsigma.spring.hibernate.persistence.domain.Photo;
import com.telkomsigma.spring.hibernate.persistence.domain.Player;
import com.telkomsigma.spring.hibernate.persistence.domain.Reserve;
import com.telkomsigma.spring.hibernate.persistence.domain.TPlayer;
import com.telkomsigma.spring.hibernate.service.PlayerService;
import com.telkomsigma.spring.hibernate.service.TeamService;

@Controller
@SessionAttributes
public class PlayerController {

	ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});

	TeamService teamDao = (TeamService) context.getBean("serviceTeam");
	PlayerService playerDao = (PlayerService) context.getBean("playerService");

	FootballTeam team = new FootballTeam();
	Player player = new Player();

	@RequestMapping(value = "/showPlayer", method = RequestMethod.GET)
	public ModelAndView showTeam(@RequestParam("id") String teamId) {
		//ambil data
		team = teamDao.findById(Integer.parseInt(teamId));
		List<TPlayer> playerList = playerDao.findByProperty("footballTeam", team, 0);
		Map<String, Object> model = new HashMap<String, Object>();

		Iterator i = playerList.iterator();
		List<Player> players = new ArrayList<Player>();
		List<Reserve> reserves = new ArrayList<Reserve>();

		while(i.hasNext()) {
			TPlayer tp = (TPlayer) i.next();
			if(tp instanceof Player){
				players.add((Player)tp);
			}else if(tp instanceof Reserve){
				reserves.add((Reserve)tp);
			}
		}

		model.put("team",team);
		model.put("players", players);
		model.put("reserves", reserves);
		return new ModelAndView("showPlayer", "model", model);

	}

	@RequestMapping(value = "/deletePlayer", method = RequestMethod.GET)
	public ModelAndView deletePlayer(@RequestParam("playerId") int playerId, 
			@RequestParam("teamId") int teamId) {

		player.setId(playerId);
		playerDao.remove(player);

		//tampilinn
		team = teamDao.findById(teamId);
		List<TPlayer> playerList = playerDao.findByProperty("footballTeam", team, 0);
		Map<String, Object> model = new HashMap<String, Object>();

		Iterator i = playerList.iterator();
		List<Player> players = new ArrayList<Player>();
		List<Reserve> reserves = new ArrayList<Reserve>();

		while(i.hasNext()) {
			TPlayer tp = (TPlayer) i.next();
			if(tp instanceof Player){
				players.add((Player)tp);
			}else if(tp instanceof Reserve){
				reserves.add((Reserve)tp);
			}
		}

		model.put("team",team);
		model.put("players", players);
		model.put("reserves", reserves);
		return new ModelAndView("showPlayer", "model", model);

	}
	//	
	@RequestMapping(value = "/addPlayer", method = RequestMethod.GET)
	public ModelAndView addPlayer(@RequestParam("teamId") int teamId) {
		team.setId(teamId);
		return new ModelAndView("addPlayer","team",team);
	}

	@RequestMapping(value = "/savePlayer", method = RequestMethod.POST)
	protected ModelAndView insertPlayer(
			@RequestParam ("firstName") String fname,
			@RequestParam ("lastName") String lname,
			@RequestParam ("teamId") int teamId,
			@RequestParam ("reserve") String reserve2,
			@RequestParam ("image") String image,
			@RequestParam ("salary") double salary ) throws IOException{

		FootballTeam team = teamDao.findById(teamId);
		List<TPlayer> playerList2 = playerDao.findAll(playerDao.countAll()-1);
		TPlayer pla = null;

		Iterator<TPlayer> i = playerList2.iterator();
		while (i.hasNext()){
			pla=i.next();
		}
		
		File file = new File(image);
		InputStream is = new BufferedInputStream(new FileInputStream("E://foto//" + file));
		Blob blob;
		
		blob = Hibernate.createBlob(is);

		if(reserve2.equalsIgnoreCase("0")){
			Photo photo = new Photo();
			photo.setData(blob);
			photo.setId(pla.getId()+1);

			Player player = new Player();
			player.setFirstName(fname);
			player.setLastName(lname);
			player.setJoinDate(new Date());
			player.setSalary(salary);
			player.setFootballTeam(team);
			player.setPhoto(photo);
			photo.setPlayer(player);

			playerDao.insert(player);
		}else{
			Photo photo = new Photo();
			photo.setData(blob);
			photo.setId(pla.getId()+1);

			Reserve reserve = new Reserve();
			reserve.setFirstName(fname);
			reserve.setLastName(lname);
			reserve.setJoinDate(new Date());
			reserve.setCommission(salary);
			reserve.setFootballTeam(team);
			reserve.setPhoto(photo);
			photo.setPlayer(reserve);

			playerDao.insert(reserve);
		}
		//nampilin
		team = teamDao.findById(teamId);
		List<TPlayer> playerList = playerDao.findByProperty("footballTeam", team, 0);
		Map<String, Object> model = new HashMap<String, Object>();

		Iterator i2 = playerList.iterator();
		List<Player> players = new ArrayList<Player>();
		List<Reserve> reserves = new ArrayList<Reserve>();

		while(i2.hasNext()) {
			TPlayer tp = (TPlayer) i2.next();
			if(tp instanceof Player){
				players.add((Player)tp);
			}else if(tp instanceof Reserve){
				reserves.add((Reserve)tp);
			}
		}

		model.put("team",team);
		model.put("players", players);
		model.put("reserves", reserves);
		return new ModelAndView("showPlayer", "model", model);
	}
	////
	@RequestMapping(value = "/updatePlayer", method = RequestMethod.GET)
	public ModelAndView updatelayer(@RequestParam("playerId") int playerId,
			@RequestParam("teamId") int teamId) {
		//player.setId(playerId);
		FootballTeam team= teamDao.findById(teamId);
		TPlayer player = playerDao.findById(playerId);
		Map<String,Object> model = new HashMap<String,Object>();

		if(player instanceof Player){
			Player player2 = (Player) player;
			Photo photo = player2.getPhoto();
			String foto = photo.getData().toString();
			model.put("photo", foto);
			model.put("status", "player");
			model.put("player", player2);
		}else{
			Reserve player2 = (Reserve) player;
			Photo photo = player2.getPhoto();
			String foto = photo.getData().toString();
			model.put("photo", foto);
			model.put("status", "reserve");
			model.put("player", player2);
		}

		model.put("team", team);

		return new ModelAndView("updatePlayer","model",model);
	}

	@RequestMapping(value = "/editPlayer", method = RequestMethod.POST)
	protected ModelAndView editPlayer(
			@RequestParam ("teamId") int teamId,
			@RequestParam ("idPlayer")int playerId,
			@RequestParam ("firstName") String fname,
			@RequestParam ("lastName") String lname,
			@RequestParam ("reserve") String reserve,
			@RequestParam ("image") String image,
			@RequestParam ("salary") double salary ) throws IOException{

		FootballTeam team = teamDao.findById(teamId);
		
		File file = new File(image);
		InputStream is = new BufferedInputStream(new FileInputStream("E://foto//" + file));
		Blob blob;
		
		blob = Hibernate.createBlob(is);
		
		if(reserve.equalsIgnoreCase("0")){

			Player player = (Player) playerDao.findById(playerId);
			player.setFirstName(fname);
			player.setLastName(lname);
			player.setSalary(salary);
			player.setFootballTeam(team);

			Photo photo = player.getPhoto();
			photo.setData(blob);

			player.setPhoto(photo);
			photo.setPlayer(player);

			playerDao.update(player);
		}else{

			Reserve res1 = (Reserve) playerDao.findById(playerId);
			res1.setFirstName(fname);
			res1.setLastName(lname);
			res1.setCommission(salary);
			res1.setFootballTeam(team);

			Photo photo = res1.getPhoto();
			photo.setData(blob);

			res1.setPhoto(photo);
			photo.setPlayer(res1);

			playerDao.update(res1);
		}

		//tampilin
		team = teamDao.findById(teamId);
		List<TPlayer> playerList = playerDao.findByProperty("footballTeam", team, 0);
		Map<String, Object> model = new HashMap<String, Object>();

		Iterator i = playerList.iterator();
		List<Player> players = new ArrayList<Player>();
		List<Reserve> reserves = new ArrayList<Reserve>();

		while(i.hasNext()) {
			TPlayer tp = (TPlayer) i.next();
			if(tp instanceof Player){
				players.add((Player)tp);
			}else if(tp instanceof Reserve){
				reserves.add((Reserve)tp);
			}
		}

		model.put("team",team);
		model.put("players", players);
		model.put("reserves", reserves);
		return new ModelAndView("showPlayer", "model", model);

	}

	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public ModelAndView detailPlayer(@RequestParam("teamId") int teamId,
			@RequestParam("playerId") int playerId) {
		List<FootballTeam> list2 = teamDao.findAll();
		TPlayer player = playerDao.findById(playerId);
		Map<String, Object> list3 = new HashMap<String, Object>();
		FootballTeam team = teamDao.findById(teamId);
		if(player instanceof Player){
			list3.put("status", "player");
		}else{
			list3.put("status", "reserve");
		}
		list3.put("team", list2);
		list3.put("team2", team);
		list3.put("player", player);

		return new ModelAndView("playerDetail", "list3", list3);
	}
}






